/**
 * @Copyright 2012, Founder Mobile Media Technology（Chongqing） Co., Ltd L.P. All
 *            rights reserved. The information contained herein is confidential
 *            and proprietary to Founder Mobile Media Technology（Chongqing） Co.,
 *            Ltd, and considered a trade secret as defined under civil and
 *            criminal statutes. Founder Mobile Media Technology（Chongqing） Co.,
 *            Ltd shall pursue its civil and criminal remedies in the event of
 *            unauthorized use or misappropriation of its trade secrets. Use of
 *            this information by anyone other than authorized employees of
 *            Founder Mobile Media Technology（Chongqing） Co., Ltd is granted
 *            only under a written non-disclosure agreement, expressly
 *            prescribing the scope and manner of such use.
 */
package com.telesound.dataimport.excel;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.g2room.test.DigitalMap;



/**
 * 
 *
 * @createTime: May 30, 2013 9:48:44 PM
 * @author: <a href="mailto:gaoxg@FOUNDER.COM.CN">Xuguo Gao</a>
 * @version: 0.1
 * @lastVersion: 0.1
 * @updateTime: 
 * @updateAuthor: <a href="mailto:gaoxg@FOUNDER.COM.CN">Xuguo Gao</a>
 * @changesSum: 
 * 
 */
public class TestMain {
	
	/** 系统配置文件位置，即conf.properties */
	public static String CONF_PROP_PATH = getConfPath();
	
	private static String getConfPath() {
		String path = ClassLoader.getSystemResource("").getPath();
			path += "com/framework/actualapp/resources/config.properties";
		return path;
	}
	
	/**
	 * 数字映射
	 * 
	 * @param l 原始数字
	 * @return 转换后的数字
	 */
	public static String map(String str) {
		int len = str.length();

		StringBuilder result = new StringBuilder();
		for (int i = len - 1, j = 0; i >= 0; i--, j++) {
			result.append(convert(str.charAt(i), j));
		}
		result = result.reverse();

		return result.toString();

	}
	
	/**
	 * 生成一位 数字
	 * 
	 * @param s
	 * @param i
	 * @return
	 */
	private static short convert(char s, int i) {
		int idx = i % nmap.length;
		short ss = Short.parseShort(s + "");
		short tars = nmap[idx][ss];
		return tars;
	}
	
	/*
	 * 
	 * ! warn 你不能修改这个数组
	 */
	private final static short[][] nmap = new short[][] { new short[] { 7, 3, 4, 1, 5, 2, 6, 0, 9, 8 }, new short[] { 1, 3, 7, 4, 6, 5, 2, 8, 9, 0 },
			new short[] { 7, 6, 1, 2, 9, 3, 5, 0, 4, 8 }, new short[] { 0, 5, 3, 6, 4, 1, 7, 9, 2, 8 }, new short[] { 6, 2, 9, 4, 1, 7, 0, 3, 5, 8 },
			new short[] { 0, 2, 4, 8, 6, 5, 7, 1, 9, 3 }, new short[] { 2, 0, 1, 7, 6, 4, 3, 5, 8, 9 }, new short[] { 5, 2, 0, 8, 7, 9, 3, 6, 4, 1 },
			new short[] { 3, 8, 9, 7, 4, 1, 5, 2, 6, 0 }, new short[] { 9, 4, 1, 7, 3, 2, 6, 8, 5, 0 } };

	public static String getNum() {
		// 取相对十年的时间
		long rs = System.nanoTime();
		for(int i=0; i<10; i++) {
			System.out.println("currentTimeMillis=="+System.currentTimeMillis());
		}
		for(int i=0; i<10; i++) {
			rs = System.nanoTime();
			System.out.println("nano=="+rs);
		}
		
		return rs + "";
	}
	// 本地Ip
	private static String LOCAL_IP = "";
	/**
	 * 获取本地机器IP
	 * @return 本地Ip
	 */
	public static String getLocalIp() {
		// 采用简单单例，出现并发也不影响。因为获取的都是一个值
		if("".equals(LOCAL_IP)) {
			try {
				InetAddress addr = InetAddress.getLocalHost();
				// 获得本机IP
				LOCAL_IP = addr.getHostAddress().toString();
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
		}
		return LOCAL_IP;
	}
	
	
	public static String genNumber() {
		String number = String.valueOf(System.nanoTime());
		// TODO number加上每台机器标识，
		// 可以采用静态变量的方式
		String localIp = getLocalIp();
		// 取最后三位 + 时间戳
		number = "".equals(localIp) ? "" : localIp.substring(localIp
				.lastIndexOf(".") + 1)
				+ number;
		System.out.println("====="+number);
		// 16 唯一序列号
		return number;

	}
	
	public static String genNumber(Long uId) {
		String number = String.valueOf(System.nanoTime());
		// TODO number加上每台机器标识，
		// 可以采用静态变量的方式
		String localIp = getLocalIp();
		// 取最后三位 + 时间戳
		number = uId + ("".equals(localIp) ? "" : localIp.substring(localIp
				.lastIndexOf(".") + 1))
				+ number;
		
		// 16 唯一序列号
		return DigitalMap.map(leftfillLength(number, 16));

	}
	
	public static String leftfillLength(String str, int i) {
		return leftfillLength(str, i, '0');
	}

	public static String leftfillLength(String str, int i, char pad) {
		if (StringUtils.isEmpty(str)) {
			return StringUtils.leftPad("", i, pad);
		}
		return StringUtils.leftPad(StringUtils.substring(str, 0, i), i, pad);

	}
	public static void main(String[] args) {
		Map<String, Boolean> numbers = new HashMap<String, Boolean>();
		for(int i =0; i< 200; i++) {
			String no = genNumber(10l);
			if(null == numbers.get(no)) {
				numbers.put(no, true);
				System.out.println("==="+no);
			} else {
				System.out.println("==="+no);
			}
		}
//		List<User> users = new ArrayList<User>();
//		for(int i = 0; i <10; i++) {
//			User u = new User();
//			u.setId(i+"");
//			u.setName("name" + i);
//			users.add(u);
//		}
//		
//		for(User u : users) {
//			u.setId("id" + u.getId());
//		}
//		
//		for(User u : users) {
//			System.out.println(u.getId());
//		}
		
		
////		System.out.println(map(genNumber()));
//		BigDecimal amount = new BigDecimal(1111);
//		BigDecimal orderAmount = new BigDecimal(4567);
//		BigDecimal scal = BigDecimal.valueOf(10);
//		BigDecimal rmb = orderAmount.subtract(amount).divide(scal).setScale(4,BigDecimal.ROUND_UP);
//		System.out.println("=" + rmb);
//		
//		InetAddress addr;
//		try {
//			addr = InetAddress.getLocalHost();
//			System.out.println(addr.getHostAddress().toString());//获得本机IP
//		} catch (UnknownHostException e) {
//			e.printStackTrace();
//		}
		
		
//		String min ="{111";
//		String max = "222]";
//		// 处理前缀和后缀符号
//		if(min.startsWith("{") || min.startsWith("[")) {
//			min = min.substring(1);
//		}
//		if(max.endsWith("}") || max.endsWith("]")) {
//			max = max.substring(0, max.length() - 1);
//		}
//		System.out.println(min+"==="+max);
//		Runtime run = Runtime.getRuntime(); 
//		long max = run.maxMemory(); 
//
//		long total = run.totalMemory(); 
//
//		long free = run.freeMemory(); 
//
//		long usable = max - total + free; 
//
//		System.out.println("最大内存 = " + (max / 1024 /1024)); 
//		System.out.println("已分配内存 = " + (total / 1024 /1024)); 
//		System.out.println("已分配内存中的剩余空间 = " + (free / 1024 /1024)); 
//		System.out.println("最大可用内存 = " + (usable / 1024 /1024));

//		Date d = getDaysAgo(2).getTime();
//		System.out.println("----=" + DateUtil.date2String(d, "yyyy-MM-dd"));
		
	}
	
	public static Calendar getDaysAgo(int type) {
		Calendar cal = Calendar.getInstance();
		if(type == 1) {
			cal.add(Calendar.DATE, -7);
		} else if(type == 2) {
			cal.add(Calendar.MONTH, -12);
		}
//		cal.set(Calendar.HOUR_OF_DAY, 0);
//		cal.set(Calendar.MINUTE, 0);
//		cal.set(Calendar.SECOND, 0);
//		cal.set(Calendar.MILLISECOND, 0);
		return cal;
	}
}
